package com.lifl.b328.garden.model;

import java.util.Observable;
import java.util.Observer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class JSONStore extends Storage {
	
	protected DatabaseHelper database;
	protected CacheDAO<NotePoolStructure> poolDAO;
	protected CacheDAO<CoupleLocalDistant> poolnoteDAO;
	protected CacheDAO<CoupleLocalDistant> pooluserDAO;
	
		public JSONStore(Context context){
			database = new DatabaseHelper(context);
			poolDAO = new NotePoolDAO();
			poolnoteDAO = new NoteLocDistDAO();
			pooluserDAO = new UserLocDistDAO();
		}

	@Override
	public void addNote(Note n, final long userId,final long noteId,final Handler handler) {
		// TODO
		String jsonString = Tools.printJson(n.getUser(), n, userId, noteId, Constantes.ACTION_JSON_CREATE_NOTE);
		MonLog.print(jsonString, getClass());
		PhotoContent pc = (PhotoContent) n.getContentByKey(Constantes.NOTE_PHOTO);
		String url;
		if(pc != null)
			url = pc.contenu;
		else
			url ="";
		Observer  obs = new Observer() {
				
				@Override
				public void update(Observable observable, Object data) {
					if(data == null){
						System.out.println("data null");
						return;
					}
					try {
						JSONObject json_data = (JSONObject) data;
						Long note = json_data.getLong("id_note");
						Long user  = json_data.getLong("id_user");
						handler.onResponse(note,user);
					} catch (JSONException e) {
						handler.onError();
					}
					
				}
			};
			System.out.println("l'url est: "+url);
			new SendJSONTask(obs).execute(Constantes.SERVER_ADRESS,jsonString,url);	
				
		Log.v("JSON STORE","connection internet envoi sur la base");
	}

	@Override
	public void deleteNote(long id,User user, final Handler handler) {
		// TODO Auto-generated method stub
		MonLog.print("jsonStore delete", getClass());
		Log.v("JSON STORE","connection internet reception de la basee");
		String jsonString = Tools.printJson(user, null, user.getID(), id, Constantes.ACTION_JSON_DELETE_NOTE);
		System.out.println(jsonString);
		Observer obs = new Observer() {
			
			@Override
			public void update(Observable observable, Object data) {
				if(data == null){
					System.out.println("data null");
					handler.onError();
					return;
				}
					handler.onResponse();
					
			}
		};
		new SendJSONTask(obs).execute(Constantes.SERVER_ADRESS,jsonString);	
		}

	@Override
	public long editNote(Note n, long id){
		//TODO
		return 0;
	}
	
	@Override
	public void getAllNotes(User user,long idUser,String order,final Handler handler) {
		String jsonString = Tools.printJson(user,null, idUser, -1, Constantes.ACTION_JSON_GETALL_NOTE);
		System.out.println("l id de l user est "+ idUser);
		MonLog.print(jsonString, getClass());
		Observer  obs = new Observer() {
			
			@Override
			public void update(Observable observable, Object data) {
				if(data == null){
					System.out.println("data null");
					return;
				}
				try {
					JSONObject json_data = (JSONObject) data;
					JSONArray note = json_data.getJSONArray("reponse");
					handler.onResponse(note);
				} catch (JSONException e) {
					handler.onError();
				}
				
			}
		};
		new SendJSONTask(obs).execute(Constantes.SERVER_ADRESS,jsonString);	
	}

	@Override
	public void getAllNotes(User user,long idUser,Handler handler){
		getAllNotes(user, idUser,null ,handler);
	}
	
	@Override
	public void addUser(User user, final Handler handler){
		String jsonString = Tools.printJson(user,null, user.id, -1, Constantes.ACTION_JSON_CREATE_USER);
		MonLog.print(jsonString, getClass());
			Observer  obs = new Observer() {
			
			@Override
			public void update(Observable observable, Object data) {
				if(data == null){
					System.out.println("data null");
					return;
				}
				try {
					JSONObject json_data = (JSONObject) data;
					Long id_user = json_data.getLong("id_user");
					System.out.println("id user distant "+id_user);
					handler.onResponse(id_user);
				} catch (JSONException e) {
					handler.onError();
				}
				
			}
		};
		new SendJSONTask(obs).execute(Constantes.SERVER_ADRESS,jsonString);	
	}
	

}

